package gdepot

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Orders)
class OrdersSpec extends Specification {

    def setup() {
        def user=new Users(userName: 'liang',
                password: '111111'.encodeAsPassword(),
                email: 'ss@qq.com',
                phone: '12345678',
                address: 'dsdsccd')
        assertNotNull(user.save(flash:true))
        def category=new Category(categoryName: 'book')
        assertNotNull(category.save(flash:true))
        def book1=new Goods(title: 'book1',price: '20.3',description: '',photoUrl: '',category: category)
        assertNotNull(book1.save(flush: true))
        def book2=new Goods(title: 'book2',price: '32.5',description: '',photoUrl: '',category: category)
        assertNotNull(book1.save(flush: true))
        def cart=new Cart(status: CartStatus.NEW,user:user)
        assertNotNull(cart.save(flash:true))
        def lineItem1=new LineItem(cart: cart,goods: book1,itemNumber: 2 )
        assertNotNull(lineItem1.save(flash:true))
        def lineItem2=new LineItem(cart: cart,goods: book2,itemNumber: 3 )
        assertNotNull(lineItem2.save(flash:true))

        cart.refresh()
        nonEmptyCartId=cart.id

        cart=new Cart(status: CartStatus.NEW,user:user)
        assertNotNull(cart.save(flash:true))
        cart.refresh()
        emptyCartId=cart.id


    }

    def cleanup() {
    }

    void testOrdersVerify() {
        def orders=new Orders(cart: Cart.get(emptyCartId))
        orders.velidate()
        assertTrue(orders.errors.hasFieldErrors('cart'))
        orders=new Orders(cart:Cart.get(nonemptyCartId))
        orders.validate()
        assertFalse(orders.errors.hasFieldErrors('cart'))
    }
}
